import { Logger } from "db://assets/util";

const logger = new Logger("服务注册表::ServiceLocator");

// 服务定位器核心类
export class ServiceLocator {
  // 服务容器（Map结构存储）
  static #services: Map<string, any> = new Map();

  public services: Map<string, any> = new Map();

  // 注册服务
  static register<T>(key: string, instance: T): void {
    this.#services.set(key, instance);
  }

  register<T>(key: string, instance: T): void {
    this.services.set(key, instance);
  }

  // 获取服务
  static get<T>(key: string): T {
    const service = this.#services.get(key);
    if (!service) {
      logger.error('服务未注册 %s', key)
    }
    return service as T;
  }

  get<T>(key: string): T {
    const service = this.services.get(key);
    if (!service) {
      logger.error('服务未注册')
    }
    return service as T;
  }
}
